package com.wsoft.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.system.entity.SysMemberOrgRoleRelationEntity;
import com.wsoft.system.entity.SysRoleEntity;
import com.wsoft.system.mapper.SysMemberOrgRoleRelationMapper;
import com.wsoft.system.mapper.SysRoleMapper;
import com.wsoft.system.query.role.RoleInsertQuery;
import com.wsoft.system.query.role.RoleListQuery;
import com.wsoft.system.query.role.RoleUpdateQuery;
import com.wsoft.system.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author juiet
 * @since 2024年09月22日 14:00:05
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRoleEntity> implements ISysRoleService {

   @Resource
    private SysMemberOrgRoleRelationMapper memberOrgRoleRelationMapper;
   @Resource
   private RedisService redisService;
    @Override
    public boolean insert(RoleInsertQuery query) {
        SysRoleEntity convert = Convert.convert(SysRoleEntity.class, query);
        return save(convert);
    }

    @Override
    public boolean update(RoleUpdateQuery query) {
        SysRoleEntity convert = Convert.convert(SysRoleEntity.class, query);
        //查询拥有该角色的用户
        QueryWrapper<SysMemberOrgRoleRelationEntity> queryWrapper =  new QueryWrapper<>();
        queryWrapper.lambda().eq(SysMemberOrgRoleRelationEntity::getRoleId,query.getId());
        List<SysMemberOrgRoleRelationEntity> entities = memberOrgRoleRelationMapper.selectList(queryWrapper);
        entities.forEach(entity -> {
            redisService.del(RedisKeyConstant.ROLE_CACHE + convert.getId());
            redisService.del(RedisKeyConstant.MEMBER_ROLE_CACHE + entity.getMemberId());
        });
        return updateById(convert);
    }

    @Override
    public boolean delete(Long id) {
        //查询拥有该角色的用户
        QueryWrapper<SysMemberOrgRoleRelationEntity> queryWrapper =  new QueryWrapper<>();
        queryWrapper.lambda().eq(SysMemberOrgRoleRelationEntity::getRoleId,id);
        List<SysMemberOrgRoleRelationEntity> entities = memberOrgRoleRelationMapper.selectList(queryWrapper);
        entities.forEach(entity -> {
            redisService.del(RedisKeyConstant.ROLE_CACHE + id);
            redisService.del(RedisKeyConstant.MEMBER_ROLE_CACHE + entity.getMemberId());
        });return removeById(id);
    }

    @Override
    @GetRedisCache(type = RedisKeyConstant.ROLE_CACHE,
            value = RedisKeyConstant.REDIS_SELF)
    public SysRoleEntity getRole(Long id) {
        return getById(id);
    }

    @Override
    public PageInfo<SysRoleEntity> pageRole(RoleListQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        QueryWrapper<SysRoleEntity> roleQuery = new QueryWrapper<>();

        if (StrUtil.isNotBlank(query.getCode())) {
            roleQuery.lambda().eq(SysRoleEntity::getCode, query.getCode());
        }
        if (StrUtil.isNotBlank(query.getName())) {
            roleQuery.lambda().like(SysRoleEntity::getName, query.getName());
        }
        return PageInfo.of(list(roleQuery));
    }
}
